package ${package}.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieyi.common.PltResult;
import com.jieyi.form.ConsoleUser;
import com.jieyi.form.map.MapForObject;
import com.jieyi.requestData.${ClassName}AddData;
import com.jieyi.requestData.${ClassName}DeleteData;
import com.jieyi.requestData.${ClassName}PagingListData;
import com.jieyi.requestData.${ClassName}UpdateData;
import com.jieyi.service.ConsoleUserService;
import com.jieyi.service.${ClassName}Service;
import com.jieyi.util.ResponseUtil;
import com.jieyi.verify.Verify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * ${tableName} ${tableComment}
 *
 * @author JieYi Generator by ${author} ${email}
 * @date ${date}
 */
@Controller
public class ${ClassName}Controller {

    @Autowired
    private ${ClassName}Service ${className}Service;

    @Autowired
    private ConsoleUserService consoleUserService;

    @PostMapping(path = "/console/${moduleName}/${className}/pagingList/${r'${msg.ver}'}")
    @ResponseBody
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        ConsoleUser consoleUser = new ConsoleUser();
        Map<String, Object> oprdata = new HashMap<>();
        String msgToken = consoleUserService.checkMsgTokenAndSign(jsonObject, consoleUser, oprdata, request);
        ${ClassName}PagingListData ${className}PagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });
        Verify.verifyData(${className}PagingListData);
        Map<String, Object> data = ${className}Service.pagingList(${className}PagingListData, consoleUser);
        return ResponseUtil.toJsonWithMsgToken(PltResult.RESULT_0000, data, msgToken);
    }

    @PostMapping(path = "/console/${moduleName}/${className}/add/${r'${msg.ver}'}")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        ConsoleUser consoleUser = new ConsoleUser();
        Map<String, Object> oprdata = new HashMap<>();
        String msgToken = consoleUserService.checkMsgTokenAndSign(jsonObject, consoleUser, oprdata, request);
        ${ClassName}AddData ${className}AddData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });
        Verify.verifyData(${className}AddData);
        Map<String, Object> data = ${className}Service.add(${className}AddData, consoleUser);
        return ResponseUtil.toJsonWithMsgToken(AppPltResult.RESULT_0000, data, msgToken);
    }

    @PostMapping(path = "/console/${moduleName}/${className}/update/${r'${msg.ver}'}")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        ConsoleUser consoleUser = new ConsoleUser();
        Map<String, Object> oprdata = new HashMap<>();
        String msgToken = consoleUserService.checkMsgTokenAndSign(jsonObject, consoleUser, oprdata, request);
        ${ClassName}UpdateData ${className}UpdateData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });
        Verify.verifyData(${className}UpdateData);
        Map<String, Object> data = ${className}Service.update(${className}UpdateData, consoleUser);
        return ResponseUtil.toJsonWithMsgToken(PltResult.RESULT_0000, data, msgToken);
    }

    @PostMapping(path = "/console/${moduleName}/${className}/delete/${r'${msg.ver}'}")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        ConsoleUser consoleUser = new ConsoleUser();
        Map<String, Object> oprdata = new HashMap<>();
        String msgToken = consoleUserService.checkMsgTokenAndSign(jsonObject, consoleUser, oprdata, request);
        ${ClassName}DeleteData ${className}DeleteData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });
        Verify.verifyData(${className}DeleteData);
        Map<String, Object> data = ${className}Service.delete(${className}DeleteData, consoleUser);
        return ResponseUtil.toJsonWithMsgToken(PltResult.RESULT_0000, data, msgToken);
    }

}